package com.bing.jh.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bing.bingPro.util.ProvinceUtil;
import com.bing.jh.domain.DzWzCostDtl;
import com.bing.jh.entity.dto.TZwArchivesDto;
import com.bing.jh.entity.vo.DzWzCostDtlVO;
import com.bing.jh.entity.vo.TZwResult;
import com.bing.jh.mapper.TZwArchivesDorisDao;
import com.bing.jh.service.DzWzCostDtlService;
import com.bing.jh.mapper.DzWzCostDtlMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author ck
* @description 针对表【dz_wz_cost_dtl】的数据库操作Service实现
* @createDate 2024-06-09 14:58:54
*/
@Service
public class DzWzCostDtlServiceImpl extends ServiceImpl<DzWzCostDtlMapper, DzWzCostDtl>
    implements DzWzCostDtlService{

    @Resource
    private TZwArchivesDorisDao tZwArchivesDorisDao;

    @Override
    public List<DzWzCostDtlVO> detail(TZwArchivesDto tZwArchives) {
        int i =0;
        tZwArchives.setIdentify(i);
        tZwArchives.setProvince(ProvinceUtil.getProvinceCode(tZwArchives.getProvinceCode()));
        List<TZwResult> tZwArchivesAndDetailsEntities = tZwArchivesDorisDao.queryRank(tZwArchives.getMonth(),tZwArchives.getIdentify());
        if (CollUtil.isEmpty(tZwArchivesAndDetailsEntities)) {
            tZwArchivesAndDetailsEntities = tZwArchivesDorisDao.queryDefault(tZwArchives.getMonth(),tZwArchives.getIdentify());
        }
        Map<String, Map<String,List<Integer>>> numMap = tZwArchivesAndDetailsEntities.stream().collect(Collectors.toMap(
                TZwResult::getProvinceName,
                v -> {
                    String scoreType = v.getScoreType();
                    Map<String, List<Integer>> map = new HashMap<>();
                    Integer num = Integer.valueOf(v.getNum());
                    List<Integer> integers = new ArrayList<>();
                    integers.add(num);

                    if (scoreType.equals("0")) {
                        map.put("0", integers);
                    } else if (scoreType.equals("1")) {
                        map.put("1", integers);
                    }
                    return map;
                },
                (v1, v2) -> {
                    initMap("0",v1,v2);
                    initMap("1",v1,v2);
                    return v1;
                }
        ));

        List<DzWzCostDtlVO> mapList = tZwArchivesDorisDao.queryDetail(tZwArchives);

        mapList.forEach(o->{
            String provinceName = o.getProvinceCode();
            Map<String, List<Integer>> stringListMap = numMap.get(provinceName);
            List<Integer> integers0 = stringListMap.get("0");
            List<Integer> integers1 = stringListMap.get("1");
            if (!CollectionUtils.isEmpty(integers0)){
                Integer rank = o.getPortraitScoreProvRank();
                //判断是哪个区间
                Integer level = getLevel(rank, integers0);
                //获取等级
                o.setPortraitScoreGrade(level);
            }
            if (!CollectionUtils.isEmpty(integers1)){
                Integer rank = o.getSalesVolumeProvRank();
                //判断是哪个区间
                Integer level = getLevel(rank, integers1);
                //获取等级
                o.setSalesVolumeGrade(level);
            }
        });
        return mapList;
    }

    @Override
    public List<String> dateList() {
        return baseMapper.dateList();
    }

    public Integer getLevel(Integer rank, List<Integer> levelRanks) {
        //排序
        levelRanks.sort(null);
        //
        for (int i = 0; i < levelRanks.size(); i++) {
            if (rank <= levelRanks.get(i)) {
                return i + 1;
            }
        }
        return levelRanks.size();
    }


    public void initMap(String type, Map<String, List<Integer>> v1, Map<String, List<Integer>> v2) {
        List<Integer> integers = v2.get(type);
        if (!CollectionUtils.isEmpty(integers)) {
            List<Integer> oldList = v1.get(type);
            if (CollectionUtils.isEmpty(oldList)) {
                oldList = new ArrayList<>();
            }
            oldList.addAll(integers);
            v1.put(type, oldList);
        }
    }
}




